package com.craftplay.dao;

import com.craftplay.model.Focus;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.List;


@Mapper
public interface FocusMapper {
    @Delete({
            "delete from focus",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Delete({
            "delete from focus",
            "where focusUserId = #{focusUserId,jdbcType=INTEGER}  and userId = #{userId,jdbcType=INTEGER}"
    })
    int deleteByFocusUserId(Focus focus);


    @Insert({

            "INSERT INTO `focus` (`focusUserId`, `userId`, `focusTime`) VALUES" +
                    " ( #{focusUserId,jdbcType=INTEGER},#{userId,jdbcType=INTEGER}, #{focusTime,jdbcType=INTEGER});"
    })
    @SelectKey(statement = "select LAST_INSERT_ID()", keyProperty = "id", before = false, resultType = int.class)
    int insert(Focus focus);


    @Select({
            "select * from  focus",
            "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusUserId", property = "focusUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusTime", property = "focusTime", jdbcType = JdbcType.TIMESTAMP),
    })
    Focus selectByPrimaryKey(Integer id);

    @Select({
            "select * from  focus",
            "where userId = #{userId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusUserId", property = "focusUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusTime", property = "focusTime", jdbcType = JdbcType.TIMESTAMP),
    })
    List<Focus> selectByUserId(Integer userId);


    @Select({
            "select * from  focus",
            "where focusUserId = #{focusUserId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusUserId", property = "focusUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusTime", property = "focusTime", jdbcType = JdbcType.TIMESTAMP),
    })
    List<Focus> selectByFocusUserId(Integer focusUserId);


    @Select({
            "select * from  focus",
            "where userId = #{userId,jdbcType=INTEGER} and focusUserId = #{focusUserId,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "userId", property = "userId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusUserId", property = "focusUserId", jdbcType = JdbcType.INTEGER),
            @Result(column = "focusTime", property = "focusTime", jdbcType = JdbcType.TIMESTAMP),
    })
    Focus selectByUserIdAndFocusId(@Param(value = "userId") Integer userId, @Param(value = "focusUserId") Integer focusUserId);


    @Select({
            "select count(*) from  focus",
            "where userId = #{userId,jdbcType=INTEGER}"
    })
    int myFocusUserNum(int userId);


    @Select({
            "select count(*) from  focus",
            "where focusUserId = #{focusUserId,jdbcType=INTEGER}"
    })
   int selectByFocusUserIdNum(int userId);
}